<!-- 验证插件 -->
<script src="/static/plugins/jquery-validation/jquery.validate.min.js"></script>
<script src="/static/plugins/jquery-validation/localization/messages_zh.min.js"></script>
<!-- 针对当前主题进行简单封装，精简代码 -->
<script src="/static/sdtheme/scripts/sdvalidate.js"></script>
<script>
    var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
    $(function () {
        //验证
        validateInfo();
    })
    //表单验证及AJAX提交
    function validateInfo() {
        var $thisForm = $('#thisForm');
        $thisForm.sdvalidate({
            rules: {
                Key: {
                    engnum: true,
                    required: true,
                },
                Value: {
                    engnum: true,
                    required: true
                },
            },
            messages: {
                Key: {
                    engnum: "名称不能为中文字符",
                    required: "请输入名称",
                },
                Value: {
                    engnum: "值不能为中文字符",
                    required: "请输入键值",
                },
            },
            submitHandler: function (form) {
                var url ='{{ urlfor "EnvController.Edit"}}'
                var data = $thisForm.serialize();
                $.sdpost(url,data,function(re){
                    if(re.code === 0){
                        parent.layer.msg(re.msg)
                        parent.refresh(re.obj)
                        parent.layer.close(index)
                    }else{
                        parent.layer.alert(re.msg,{icon:2,title:'失败'})
                    }
                });
                return false;
            }
        });
    }
    jQuery.validator.addMethod("engnum", function(value, element) {
        return this.optional(element) || (/^[^\u4e00-\u9fa5]+$/.test(value) );
    }, "请输入合法的IP信息");
    jQuery.validator.addMethod("ip", function(value, element) {
        return this.optional(element) || (/^(\d+)\.(\d+)\.(\d+)\.(\d+)$/.test(value) && (RegExp.$1 <256 && RegExp.$2<256 && RegExp.$3<256 && RegExp.$4<256));
    }, "请输入合法的IP信息");

    function FormSubmit() {
        $("#thisForm").submit();
    }
</script>